﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace iRemoteMulti.Forms
{
    public partial class Channel : Form
    {
        public int index = 0;

        public int time_count = 0;
        public bool port_is_open = false;

        public int show_status_seconds = 30;

        public Channel(int index)
        {
            InitializeComponent();
            //drawGraph();
            this.index = index;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn1 = (Button)sender;
            if (btn1.Text.Equals("ON"))
            {
                iRemote.sendDataToPort(0xC1,-1,this.index);
            }
            else if (btn1.Text.Equals("OFF"))
            {
                iRemote.sendDataToPort(0xC2, -1, this.index);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Button btn2 = (Button)sender;
            if (btn2.Text.Equals("ENABLE"))
            {
                iRemote.sendDataToPort(0xC3, -1, this.index);
            }
            else if (btn2.Text.Equals("RESET"))
            {
                iRemote.sendDataToPort(0xC4, -1, this.index);
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (!groupBox4.Enabled)
            {
                groupBox4.Enabled = true;
            }
            else
            {
                groupBox4.Enabled = false;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Button btn4 = (Button)sender;
            if (btn4.Text.Equals("Emergency Stop"))
            {
                iRemote.sendDataToPort(0xC9, -1, this.index);
            }
            else if (btn4.Text.Equals("Reset Emergency Stop"))
            {
                iRemote.sendDataToPort(0xCA, -1, this.index);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Value = 0;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Value = 250;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Value = 500;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Value = 750;
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            trackBar1.Value = 1000;
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            textBox7.Text = (Convert.ToDouble(trackBar1.Value)/10).ToString("f1");
        }

        private void textBox7_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Return)
            {
                set_gain_value();
            }
        }

        private bool set_gain_value() {
            try
            {
                Double num = Convert.ToDouble(textBox7.Text);
                if (num > 100 || num < 0)
                {
                    MessageBox.Show("Please input right num!");
                    textBox7.Text = (Convert.ToDouble(trackBar1.Value) / 10).ToString("f1");
                    return false;
                }
                else
                {
                    textBox7.Text = num.ToString("f1");
                    trackBar1.Value = Convert.ToInt32(num * 10);
                    return true;
                }
            }
            catch
            {
                MessageBox.Show("Please input right num!");
                textBox7.Text = (Convert.ToDouble(trackBar1.Value) / 10).ToString("f1");
                return false;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (set_gain_value())
            {
                double value = (double)(trackBar1.Value) / 1000;
                iRemote.sendDataToPort(0xC5, value, this.index);
                groupBox4.Enabled = false;
            }
                
        
        }

        public string convertSecondToTime(int seconds)
        {
            int h = seconds / 3600;
            int m = (seconds - 3600 * h) / 60;
            int s = (seconds - 3600 * h) % 60;

            string len = h.ToString().PadLeft(2, '0') + ":" + m.ToString().PadLeft(2, '0') + ":" + s.ToString().PadLeft(2, '0');

            return len;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label15.Text = convertSecondToTime(time_count);
            if (time_count % 3600 == 0) {
                if (iRemote.ports[iRemote.current_index].IsOpen)
                {
                    iRemote.ports[iRemote.current_index].Close();
                    iRemote.buffer[iRemote.current_index].Clear();
                    iRemote.ports[iRemote.current_index].Open();
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (port_is_open)
            {
                if (!timer1.Enabled )
                {
                    label15.Text = convertSecondToTime(time_count);
                    timer1.Enabled = true;
                }
                
            }
            else
            {
                if (timer1.Enabled)
                {
                    timer1.Enabled = false;
                }
            }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            byte operation = 0xC6;
            double data = -1;
            iRemote.circularSendDataToPort(operation, data, this.index);

        }
        public void reset_light() 
        {
            pictureBox1.Image = Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\Image\off.png");
            pictureBox2.Image = Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\Image\off.png");
            pictureBox3.Image = Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\Image\off.png");
            pictureBox4.Image = Image.FromFile(System.Windows.Forms.Application.StartupPath + @"\Image\off.png");
        }

        /*
        public void drawGraph()
        {
            ZedGraphControl zgc = zedGraphControl1;
            GraphPane myPane = zgc.GraphPane;

            // Set the titles and axis labels
            myPane.Title.Text = "Display of status";
            myPane.XAxis.Title.Text = "Time, s";
            myPane.YAxis.Title.Text = "Velocity, V";
            myPane.Y2Axis.Title.Text = "Acceleration, A";
            myPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;

            FontSpec titleFont = new FontSpec("Arial", 24, Color.Black, true, false, false);
            FontSpec labelFont = new FontSpec("Arial", 15, Color.Black, false, false, false);
            
            myPane.Title.FontSpec = titleFont;
            myPane.XAxis.Title.FontSpec = titleFont;
            myPane.YAxis.Title.FontSpec = titleFont;
            myPane.Y2Axis.Title.FontSpec = titleFont;
            // Generate a red curve with diamond symbols, and "Velocity" in the legend
            LineItem myCurve = myPane.AddCurve("Output Voltage", output_voltage, Color.Red, SymbolType.Diamond);
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Set the curve label font
            myCurve.Label.FontSpec = labelFont;
            

            // Generate a blue curve with circle symbols, and "Acceleration" in the legend
            myCurve = myPane.AddCurve("Output Current",output_current, Color.Blue, SymbolType.Circle);
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Set the curve label font
            myCurve.Label.FontSpec = labelFont;
            // Associate this curve with the Y2 axis
            myCurve.IsY2Axis = true;

            // Generate a green curve with square symbols, and "Distance" in the legend
            myCurve = myPane.AddCurve("Filed Voltage",field_voltage, Color.Green, SymbolType.Square);
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Set the curve label font
            myCurve.Label.FontSpec = labelFont;
            // Associate this curve with the second Y axis
            //myCurve.YAxisIndex = 1;

            // Generate a Black curve with triangle symbols, and "Energy" in the legend
            myCurve = myPane.AddCurve("Field Current",field_current, Color.Black, SymbolType.Triangle);
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Set the curve label font
            myCurve.Label.FontSpec = labelFont;
            // Associate this curve with the Y2 axis
            myCurve.IsY2Axis = true;
          // Associate this curve with the second Y2 axis
            //myCurve.YAxisIndex = 1;

            // Generate a red curve with diamond symbols, and "Velocity" in the legend
            myCurve = myPane.AddCurve("Main Voltage/Power Supply", main_voltage, Color.Violet, SymbolType.TriangleDown);
            // Fill the symbols with white
            myCurve.Symbol.Fill = new Fill(Color.White);
            // Set the curve label font
            myCurve.Label.FontSpec = labelFont;


            // Show the x axis grid
            myPane.XAxis.MajorGrid.IsVisible = true;

            // Make the Y axis scale red
           // myPane.YAxis.Scale.FontSpec.FontColor = Color.Red;
           //myPane.YAxis.Title.FontSpec.FontColor = Color.Red;
            // turn off the opposite tics so the Y tics don't show up on the Y2 axis
            myPane.YAxis.MajorTic.IsOpposite = false;
            myPane.YAxis.MinorTic.IsOpposite = false;
            // Don't display the Y zero line
            myPane.YAxis.MajorGrid.IsZeroLine = false;
            // Align the Y axis labels so they are flush to the axis
            myPane.YAxis.Scale.Align = AlignP.Inside;
            myPane.XAxis.Scale.Min = 0;
            myPane.YAxis.Scale.Max = 250;

            // Enable the Y2 axis display
            myPane.Y2Axis.IsVisible = true;
            // Make the Y2 axis scale blue
            //myPane.Y2Axis.Scale.FontSpec.FontColor = Color.Blue;
            //myPane.Y2Axis.Title.FontSpec.FontColor = Color.Blue;
            // turn off the opposite tics so the Y2 tics don't show up on the Y axis
            myPane.Y2Axis.MajorTic.IsOpposite = false;
            myPane.Y2Axis.MinorTic.IsOpposite = false;
            // Display the Y2 axis grid lines
            myPane.Y2Axis.MajorGrid.IsVisible = true;
            // Align the Y2 axis labels so they are flush to the axis
            myPane.Y2Axis.Scale.Align = AlignP.Inside;
            myPane.Y2Axis.Scale.Min = 0;
            myPane.Y2Axis.Scale.Max = 500;

            // Fill the axis background with a gradient
            myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);

            zgc.AxisChange();
            zgc.Refresh();
            //zgc.Invalidate();
        }
        */

        /*
        public void clearDraw() 
        {
            ZedGraphControl zgc = zedGraphControl1;
            GraphPane myPane = zgc.GraphPane;
            myPane.CurveList.Clear();

            zgc.AxisChange();
            zgc.Refresh();

            output_voltage.Clear();
            output_current.Clear();
            field_voltage.Clear();
            field_current.Clear();
            main_voltage.Clear();
        }
        */

        /*
        private void timer4_Tick(object sender, EventArgs e)
        {
            ZedGraphControl zgc = zedGraphControl1;
            zgc.GraphPane.XAxis.Scale.MaxAuto = true;
            double x = (double)new XDate(DateTime.Now);
            double y_output_voltage = Convert.ToDouble(textBox1.Text);
            double y_output_current = Convert.ToDouble(textBox2.Text);
            double y_field_voltage = Convert.ToDouble(textBox3.Text);
            double y_field_current = Convert.ToDouble(textBox4.Text);
            double y_main_voltage = Convert.ToDouble(textBox5.Text);
            output_voltage.Add(x, y_output_voltage);
            if (output_voltage.Count >= show_status_seconds)
            {
                output_voltage.RemoveAt(0);
            }

            output_current.Add(x, y_output_current);
            if (output_current.Count >= show_status_seconds)
            {
                output_current.RemoveAt(0);
            }

            field_voltage.Add(x, y_field_voltage);
            if (field_voltage.Count >= show_status_seconds)
            {
                field_voltage.RemoveAt(0);
            }

            field_current.Add(x, y_field_current);
            if (field_current.Count >= show_status_seconds)
            {
                field_current.RemoveAt(0);
            }

            main_voltage.Add(x, y_main_voltage);
            if (main_voltage.Count >= show_status_seconds)
            {
                main_voltage.RemoveAt(0);
            }
            zgc.AxisChange();
            zgc.Refresh();
        }
        */
    }
}
